﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace Galaktika.BI.Silverlight.ClientApp.Items
{
    public class VerticalExpander : Grid
    {
        //private TextBlock m_Caption;
        private Button m_ExpandButton;
        //private ContentControl m_Content;
        ColumnDefinition headerColumnDefinition;

        public VerticalExpander()
        {
            headerColumnDefinition = new ColumnDefinition();
            headerColumnDefinition.Width = GridLength.Auto;
            base.ColumnDefinitions.Add(headerColumnDefinition);
            base.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(0.0, GridUnitType.Pixel) });
            base.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });

            //StackPanel header = new StackPanel();
            //header.Orientation = Orientation.Vertical;
            //base.Children.Add(header);

            m_ExpandButton = new Button();
            m_ExpandButton.Style = App.Current.Resources["ButtonForm"] as Style;
            m_ExpandButton.Width = 8.0;
            //Image img = new Image();
            //img.Source = new BitmapImage(new Uri("/Galaktika.BI.Silverlight.ClientApp;component/Items/Images/Forward-double.png", UriKind.Relative));
            //m_ExpandButton.Content = img;
            //m_ExpandButton.Width = 20;
            //m_ExpandButton.Height = 20;
            m_ExpandButton.Click += new RoutedEventHandler(ExpandButton_Click);
            base.Children.Add(m_ExpandButton);
            //header.Children.Add(m_ExpandButton);

            //m_Caption = new TextBlock();
            //m_Caption.RenderTransform = new RotateTransform() { Angle = 90, CenterX = 5, CenterY = 20};
            //header.Children.Add(m_Caption);

        }

        public GridLength HeaderWidth
        {
            set
            {
                headerColumnDefinition.Width = value;
            }
            get
            {
                return headerColumnDefinition.Width; 
            }
        }

        public event EventHandler ExpandChanged;
        protected void OnExpandChanged()
        {
            if (this.ExpandChanged != null)
            {
                this.ExpandChanged(this, EventArgs.Empty);
            }
        }

        void ExpandButton_Click(object sender, RoutedEventArgs e)
        {
            this.IsExpanded = !this.IsExpanded;
        }

        private void UpdateView()
        {
            if (m_ExpandButton == null || m_Content == null) return;
            if (m_IsExpanded)
            {
                Image img = new Image();
                img.Source = new BitmapImage(new Uri("/Galaktika.BI.Silverlight.ClientApp;component/Items/Images/Back-double.png", UriKind.Relative));
                m_ExpandButton.Content = img;
                base.ColumnDefinitions[1].Width = new GridLength(1.0, GridUnitType.Star);
                m_Content.Visibility = Visibility.Visible;
                this.RestoreWidth();
            }
            else
            {
                Image img = new Image();
                img.Source = new BitmapImage(new Uri("/Galaktika.BI.Silverlight.ClientApp;component/Items/Images/Forward-double.png", UriKind.Relative));
                m_ExpandButton.Content = img;
                base.ColumnDefinitions[1].Width = new GridLength(0.0);
                m_Content.Visibility = Visibility.Collapsed;
                this.SaveWidth();
            }
        }

        class GridLengthHolder
        {
            public GridLengthHolder()
            {
            }

            public GridLengthHolder(GridLength length)
            {
                this.Length = length;
            }

            public GridLength Length;
        }

        private GridLengthHolder m_Width;
        private void SaveWidth()
        {
            if (this.Parent == null) return;
            Grid parent = this.Parent as Grid;
            if (parent == null)
            {
                FrameworkElement border = this.Parent as FrameworkElement;
                if (border != null)
                {
                    parent = border.Parent as Grid;
                }
            }
            if (parent != null)
            {
                int columnIndex = Grid.GetColumn(this);
                if (columnIndex < parent.ColumnDefinitions.Count)
                {
                    m_Width = new GridLengthHolder(parent.ColumnDefinitions[columnIndex].Width);
                    parent.ColumnDefinitions[columnIndex].Width = GridLength.Auto;
                }
            }
        }

        private void RestoreWidth()
        {
            if (this.Parent == null) return;
            Grid parent = this.Parent as Grid;
            if (parent == null)
            {
                FrameworkElement border = this.Parent as FrameworkElement;
                if (border != null)
                {
                    parent = border.Parent as Grid;
                }
            }
            if (parent != null)
            {
                int columnIndex = Grid.GetColumn(this);
                if (columnIndex < parent.ColumnDefinitions.Count)
                {
                    if (m_Width != null)
                    {
                        parent.ColumnDefinitions[columnIndex].Width = m_Width.Length;
                    }
                    else
                    {
                        parent.ColumnDefinitions[columnIndex].Width = new GridLength(200.0);
                    }
                }
            }
        }

        private bool m_IsExpanded = false;
        public bool IsExpanded
        {
            get
            {
                return m_IsExpanded;
            }
            set
            {
                if (m_IsExpanded != value)
                {
                    m_IsExpanded = value;
                    this.UpdateView();
                    this.OnExpandChanged();
                }
            }
        }

        //public string Caption
        //{
        //    get
        //    {
        //        return m_Caption.Text;
        //    }
        //    set
        //    {
        //        m_Caption.Text = value;
        //    }
        //}

        private FrameworkElement m_Content;
        public FrameworkElement Content
        {
            get
            {
                return m_Content;
            }
            set
            {
                if (m_Content != null)
                {
                    base.Children.Remove(m_Content);
                }
                m_Content = value;
                m_Content.HorizontalAlignment = HorizontalAlignment.Stretch;
                m_Content.VerticalAlignment = VerticalAlignment.Stretch;
                m_Content.Visibility = Visibility.Collapsed;
                Grid.SetColumn(m_Content, 1);
                base.Children.Add(m_Content);
            }
        }

        public void SetButtonTooltip(object text)
        {
            ToolTipService.SetToolTip(m_ExpandButton, text);
        }

        //public object Content
        //{
        //    get
        //    {
        //        return m_Content.Content;
        //    }
        //    set
        //    {
        //        m_Content.Content = value;
        //    }
        //}
    }
}
